package com.situ.ymall.admin.controller;

import javax.servlet.http.HttpSession;

import com.situ.ymall.admin.entity.Root;
import com.situ.ymall.admin.entity.APIResult;
import com.situ.ymall.admin.service.RootService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/*
 * 管理员Controller
 */
@RestController
@RequestMapping("/api/roots")
@CrossOrigin(origins = {"*"})
//使用
public class RootController {
	
	@Autowired
	private RootService rootService;
	
		/*
		 * 登录 查询
		 */
	@GetMapping
	public APIResult login(Root root, String vercode,
						   HttpSession session) {
		// 获取参数
		// 判断vercode是否正确
		
		// 调用service层
		APIResult apiResult = new APIResult();
		try {
			Root loginRoot = rootService.login(root);
			//清除密码数据
			loginRoot.setPassword(null);
			//登录成功
			session.setAttribute("root", loginRoot);
			apiResult.setStatus(200);
			apiResult.setData(loginRoot);
		} catch (Exception e) {
			e.printStackTrace();
			//登录失败  返回数据
			apiResult.setStatus(404);
			apiResult.setMsg(e.getMessage());
		}
		// 返回数据
		return apiResult;
	}
	/*
	 * 退出
	 */
	@DeleteMapping
	public APIResult logout(HttpSession session) {
		session.invalidate();
		APIResult apiResult = new APIResult();
		apiResult.setStatus(204);
		return apiResult;
	}
	/*
	 * 修改
	 */
}
